<!--
 * @Author: sjt
 * @Date: 2022-10-25 13:18:12
 * @LastEditors: sjt
 * @LastEditTime: 2024-05-16 19:57:24
 * @Description: file content
-->
<template>
  <div class="barsChart" ref="barsChartRef"></div>
</template>

<script setup>
import { defineExpose, ref } from 'vue'
import * as echarts from 'echarts'
let charts = null
const barsChartRef = ref(null)
const initChart = (datas) => {
  let xAxis = datas.map((item) => item.name.replace(/-/g, '/'))
  let option = {
    color: ['#ffc20e', '#00ae9d'],
    grid: {
      top: '10%',
      left: '2%',
      right: '5%',
      bottom: '4%',
      containLabel: true
    },
    tooltip: {
      show: false,
      trigger: 'axis',
      backgroundColor: 'rgba(61, 85, 102, 0.2)',
      borderWidth: 1,
      borderColor: '#9DBAE1', // 边框颜色
      // 选中线颜色
      axisPointer: {
        lineStyle: {
          color: '#9DBAE1'
        }
      },
      // 字体颜色
      textStyle: {
        color: '#fff',
        fontSize: 10
      }
    },
    legend: {
      right: '3%',
      icon: 'circle',
      itemWidth: 8,
      itemGap: 20,
      textStyle: {
        padding: [0, 0, 0, 5]
      }
    },
    xAxis: [
      {
        type: 'category',
        boundaryGap: false,
        axisLabel: {
          color: '#C7DFFC',
          fontSize: 18
        },
        axisLine: {
          lineStyle: {
            color: 'rgba(67, 168, 253,0.2)',
            width: 1
          }
        },
        data: xAxis
      }
    ],
    yAxis: [
      {
        type: 'value',
        name: '',
        axisLabel: {
          color: '#C7DFFC',
          fontSize: 18
        },
        splitLine: {
          lineStyle: {
            type: 'solid',
            color: 'rgba(67, 168, 253,0.1)',
            width: 2
          }
        }
      }
    ],
    series: [
      {
        name: '',
        type: 'line',
        smooth: true, // 是否平滑曲线显示
        // symbol:'circle',  // 默认是空心圆（中间是白色的），改成实心圆
        symbolSize: 0,
        lineStyle: {
          color: '#ffc20e' // 线条颜色
        },
        label: {
          show: true,
          position: 'top',
          fontSize: 18,
          color: '#ffc20e',
          formatter: function (d) {
            return d.value
          }
        },
        areaStyle: {
          // 区域填充样式
          // 线性渐变，前4个参数分别是x0,y0,x2,y2(范围0~1);相当于图形包围盒中的百分比。如果最后一个参数是‘true’，则该四个值是绝对像素位置。
          color: new echarts.graphic.LinearGradient(
            0,
            0,
            0,
            1,
            [
              { offset: 0, color: 'rgba(255,194,14, 0.8)' },
              { offset: 1, color: 'rgba(255,194,14, 0)' }
            ],
            false
          )
          // shadowColor: "rgba(53,142,215, 0.9)", // 阴影颜色
          // shadowBlur: 20 // shadowBlur设图形阴影的模糊大小。配合shadowColor,shadowOffsetX/Y, 设置图形的阴影效果。
        },
        data: datas
      }
    ]
  }

  if (!charts) {
    charts = echarts.init(barsChartRef.value)
    charts.setOption(option)
  } else {
    charts.dispose()
    charts = echarts.init(barsChartRef.value)
    charts.setOption(option)
  }
  window.addEventListener('resize', () => {
    if (charts) {
      charts.resize()
    }
  })
}

defineExpose({
  initChart
})
</script>

<style lang="scss" scoped>
.barsChart {
  width: 100%;
  height: 100%;
  overflow: hidden;
}
</style>
